﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KeshaParser
{
    public enum TokenType : sbyte
    {
        EOF = - 127,
        ERROR = -1,        
        DAY = 0,
        KESHE1,
        SVOBODU,
        KESHE,
        PIASTRI,
        NU,
        TI,
        KASHI,
        OH,
        UGASS
    }

    class Token : GrammarItem
    {
        public TokenType Type { get; set; }
        public string Attribute { get; set; }

        public Token(TokenType type, string value)
        {
            this.Type = type;
            this.Attribute = value;              
        }

        override public string ToString()
        {
            //string optPart = (this.Attribute.Length == 0) ? "" : ", \"" + this.Attribute + "\"";
            //return "<" + this.Type + optPart + ">";
            return Attribute;
        }

        override public int GetHashCode()
        {
            return this.Type.GetHashCode() + this.Attribute.GetHashCode();
        }

        override public bool Equals(object obj)
        {
            Token otherToken = (Token)obj;
            if (otherToken == null)
            {
                return base.Equals(obj);
            }

            return (this.Type == otherToken.Type)
                    && (this.Attribute.CompareTo(otherToken.Attribute) == 0);
        }

        static public Token CreateEofToken()
        {
            return new Token(TokenType.EOF, "$");
        }
    }

    static class KeywordTokenCreator
    {
        private static Dictionary<string, TokenType> keywords = new Dictionary<string, TokenType>() 
        {
            {"Day",     TokenType.DAY},
            {"Keshe1",  TokenType.KESHE1},
            {"Svobodu", TokenType.SVOBODU},
            {"Keshe",   TokenType.KESHE},
            {"Piastri", TokenType.PIASTRI},
            {"Nu",      TokenType.NU},
            {"Ti",      TokenType.TI},
            {"Kashi",   TokenType.KASHI},
            {"Oh",      TokenType.OH},
            {"Ugass",   TokenType.UGASS},
            {"$",       TokenType.EOF}
        };

        public static Token CreateByString(string strKeyword)
        {
            TokenType type = keywords.ContainsKey(strKeyword) ? keywords[strKeyword] 
                                                              : TokenType.ERROR;            
            return new Token(type, strKeyword);
        }
    }
}
